'use strict';
require('./index.css');
var _mm             = require('util/mm.js');
var templateIndex   = require('./index.string');
var _user           = require('service/user-service.js');
// 侧边导航
var navSide = {
	
    option : {
        name : '',
        navList :
		 [
            {name : 'user-center', desc : '个人中心', href: './user-center.html'},
            {name : 'order-list', desc : '我的订单', href: './order-list.html'},
            {name : 'user-pass-update', desc : '修改密码', href: './user-pass-update.html'},
			{name : 'test-list', desc : '分数详情', href: './test-list.html'},
			{name : 'become-tutor', desc : '申请成为教员', href: './become-tutor.html'},
            {name : 'about', desc : '教育资讯', href: './about.html'}		
        ],
		navListTutor :
		[
			{name : 'user-center', desc : '个人中心', href: './user-center.html'},
			{name : 'order-list', desc : '我的订单', href: './order-list.html'},
			{name : 'user-pass-update', desc : '修改密码', href: './user-pass-update.html'},
			{name : 'about', desc : '教育资讯', href: './about.html'},
		]
    },
    init : function(option){
        // 合并选项
        $.extend(this.option, option);
        this.renderNav();
		
    },
    // 渲染导航菜单
    renderNav : function(){
		var _this           = this;
		_user.getUserInfo(function(res){
			if(res.user.role == 2){
				// 计算active数据
				for(var i = 0, iLength = _this.option.navListTutor.length; i < iLength; i++){
				    if(_this.option.navListTutor[i].name === _this.option.name){
				        _this.option.navListTutor[i].isActive = true;
				    }
				};
				// 渲染list数据
				var navHtml = _mm.renderHtml(templateIndex, {
				    navListTutor : _this.option.navListTutor
				});
				// 把html放入容器
				$('.nav-side').html(navHtml);
			}else{
				// 计算active数据
				for(var i = 0, iLength = _this.option.navList.length; i < iLength; i++){
				    if(_this.option.navList[i].name === _this.option.name){
				        _this.option.navList[i].isActive = true;
				    }
				};
				// 渲染list数据
				var navHtml = _mm.renderHtml(templateIndex, {
				    navList : _this.option.navList
				});
				// 把html放入容器
				$('.nav-side').html(navHtml);
			}
		});
    }
};

module.exports = navSide;